package com.myself.model.demo.netty.foundation;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;

import java.time.LocalDateTime;
/**
 * @author Czx
 */

public class NettyServerInboundHandler implements ChannelInboundHandler {
    @Override
    public void channelRegistered(ChannelHandlerContext channelHandlerContext)  {

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext channelHandlerContext)  {

    }

    @Override
    public void channelActive(ChannelHandlerContext channelHandlerContext) {

    }

    @Override
    public void channelInactive(ChannelHandlerContext channelHandlerContext)  {

    }

    @Override
    public void channelRead(ChannelHandlerContext channelHandlerContext, Object o){
        System.out.println("客户端发来的消息:" + o);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext channelHandlerContext) {
        channelHandlerContext.writeAndFlush("服务端-接收成功返回:" + LocalDateTime.now());
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext channelHandlerContext, Object o)  {

    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext channelHandlerContext)  {

    }

    @Override
    public void handlerAdded(ChannelHandlerContext channelHandlerContext)  {

    }

    @Override
    public void handlerRemoved(ChannelHandlerContext channelHandlerContext) {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable throwable)  {
        throwable.printStackTrace();
        channelHandlerContext.close();
    }
}
